#include <buola/gui.h>

#include <buola/gui/cimagematrix.h>
#include <buola/image/cimage.h>
#include <buola/image/cgc.h>

namespace buola { namespace gui {

CImageMatrix::CImageMatrix()
{
}

CImageMatrix::~CImageMatrix()
{
}

//rectangle with size of each border
void CImageMatrix::CreateWithBorder(img::CImage_rgba8 &&pI,const CRect_d &pR)
{
    CSize_d lS=pI.Size();

    Create(std::move(pI),CRect_d(pR.l,pR.t,lS.x-pR.r,lS.y-pR.b));

    mFlags=FLAG_NONE;
}

//rectangle of the center tile
void CImageMatrix::Create(img::CImage_rgba8 &&pI,const CRect_d &pR)
{
    CSize_d lS=pI.Size();

    ///\todo
    mImage=img::CPicture(std::move(pI));

    mRect=pR;
    mSize=lS;

    mFlags=FLAG_NONE;
}

void CImageMatrix::Create3H(const img::CView_rgba8 &pW,const img::CView_rgba8 &pC,const img::CView_rgba8 &pE)
{
    msg_warn() << "create3H!!!\n";
    mImage=img::CPicture(clone(pC));

    mSize=mImage.Size();
    mRect={nZero,mSize};

    mFlags=NOTOP|NOBOTTOM;
}

void CImageMatrix::Create3V(const img::CView_rgba8 &pN,const img::CView_rgba8 &pC,const img::CView_rgba8 &pS)
{
    img::CImage_rgba8 lImage({pN.Width(),pN.Height()+pC.Height()+pS.Height()});
    img::copy_pixels(pN,img::sub_view(lImage,{0,0},pN.Size()));
    img::copy_pixels(pC,img::sub_view(lImage,{0,pN.Height()},pC.Size()));
    img::copy_pixels(pS,img::sub_view(lImage,{0,pN.Height()+pC.Height()},pS.Size()));
    
    mImage=img::CPicture(std::move(lImage));
    
    mSize=mImage.Size();
    mRect=CRect_d(0,pN.Height(),mSize.x,pN.Height()+pC.Height());

    mFlags=NOLEFT|NORIGHT;
}

void CImageMatrix::CreateRoundRect(const img::CColor& /*pColor*/,int pR)
{
    msg_warn() << "createRoundRect!!!\n";
///\todo
//  mSE=new CImage;
//  mSE->CreateQuadrant(pR,pColor);
//
//  mSW=new CImage;
//  mSW->Create(mSE);
//  mSW->FlipHorz();
//
//  mNW=new CImage;
//  mNW->Create(mSW);
//  mNW->FlipVert();
//
//  mNE=new CImage;
//  mNE->Create(mSE);
//  mNE->FlipVert();
//
//  mS=new CImage;
//    mS->Create(pColor,CSize_i(1,pR),IMAGE_FORMAT_ARGB32);
//
//  mN=new CImage;
//    mN->Create(pColor,CSize_i(1,pR),IMAGE_FORMAT_ARGB32);
//
//  mW=new CImage;
//    mW->Create(pColor,CSize_i(pR,1),IMAGE_FORMAT_ARGB32);
//
//  mE=new CImage;
//    mE->Create(pColor,CSize_i(pR,1),IMAGE_FORMAT_ARGB32);
//
//  mC=new CImage;
//    mC->Create(pColor,CSize_i(1,1),IMAGE_FORMAT_ARGB32);
//
    mSize=CSize_d(2*pR+1,2*pR+1);
    mRect=CRect_d(pR,pR,pR+1,pR+1);
}

void CImageMatrix::CreateRoundFrame(const img::CColor& /*pColor*/,int /*pRI*/,int pRO,
                                    const img::CColor& /*pColorIn*/)
{
    msg_warn() << "createRoundFrame!!!\n";
        ///\todo
//  mSE=new CImage;
//  mSE->CreateQuadrantFrame(pRI,pRO,pColor,pColorIn);
//
//  mSW=new CImage;
//  mSW->Create(mSE);
//  mSW->FlipHorz();
//
//  mNW=new CImage;
//  mNW->Create(mSW);
//  mNW->FlipVert();
//
//  mNE=new CImage;
//  mNE->Create(mSE);
//  mNE->FlipVert();
//
//  mS=new CImage;
//    mS->Create(pColor,CSize_i(1,pRO),IMAGE_FORMAT_ARGB32);
//  for(int i=0;i<pRI;i++)
//      mS->PutPixel(0,i,pColorIn.GetRGBA());
//
//  mN=new CImage;
//  mN->Create(mS);
//  mN->FlipVert();
//
//  mE=new CImage;
//    mE->Create(pColor,CSize_i(pRO,1),IMAGE_FORMAT_ARGB32);
//  for(int i=0;i<pRI;i++)
//      mE->PutPixel(i,0,pColorIn.GetRGBA());
//
//  mW=new CImage;
//  mW->Create(mE);
//  mW->FlipHorz();
//
//  mC=new CImage;
//    mC->Create(pColorIn,CSize_i(1,1),IMAGE_FORMAT_ARGB32);

    mSize=CSize_d(2*pRO+1,2*pRO+1);
    mRect=CRect_d(pRO,pRO,pRO+1,pRO+1);
}

void CImageMatrix::Draw(img::CGC *pGC,const CRect_d &pR,EFlags pFlags)
{
    //rect formed by the size of each border tile
    CRect_d lB(mRect.l,mRect.t,mSize.x-mRect.r,mSize.y-mRect.b);
    CRect_d lDest(CPoint_d(pR.l+lB.l,pR.t+lB.t),
                      CSize_d(pR.w()-lB.l-lB.r,pR.h()-lB.t-lB.b));

    if(lDest.b<lDest.t)
        lDest.b=lDest.t;
    if(lDest.r<lDest.l)
        lDest.r=lDest.l;

    EFlags lFlags=mFlags|pFlags;
    
    if(lFlags&NOLEFT)
        lDest.l=pR.l;
    if(lFlags&NORIGHT)
        lDest.r=pR.r;
    if(lFlags&NOTOP)
        lDest.t=pR.t;
    if(lFlags&NOBOTTOM)
        lDest.b=pR.b;

    //top left
    if(!(lFlags&(NOTOP|NOLEFT)))
        pGC->DrawImagePart(CRect_d(0,0,mRect.l,mRect.t),CPoint_d(pR.l,pR.t),mImage);
    //top
    if(!(lFlags&NOTOP))
        pGC->DrawImageRect(CRect_d(mRect.l,0,mRect.r,mRect.t),CRect_d(lDest.l,pR.t,lDest.r,lDest.t),mImage);
    //top right
    if(!(lFlags&(NOTOP|NORIGHT)))
        pGC->DrawImagePart(CRect_d(mRect.r,0,mSize.x,mRect.t),CPoint_d(lDest.r,pR.t),mImage);
    //left
    if(!(lFlags&NOLEFT))
        pGC->DrawImageRect(CRect_d(0,mRect.t,mRect.r,mRect.b),CRect_d(pR.l,lDest.t,lDest.l,lDest.b),mImage);
    //center
    pGC->DrawImageRect(mRect,lDest,mImage);
    //left
    if(!(lFlags&NORIGHT))
        pGC->DrawImageRect(CRect_d(mRect.r,mRect.t,mSize.x,mRect.b),CRect_d(lDest.r,lDest.t,pR.r,lDest.b),mImage);
    //bottom left
    if(!(lFlags&(NOBOTTOM|NOLEFT)))
        pGC->DrawImagePart(CRect_d(0,mRect.b,mRect.l,mSize.y),CPoint_d(pR.l,lDest.b),mImage);
    //bottom
    if(!(lFlags&NOBOTTOM))
        pGC->DrawImageRect(CRect_d(mRect.l,mRect.b,mRect.r,mSize.y),CRect_d(lDest.l,lDest.b,lDest.r,pR.b),mImage);
    //bottom right
    if(!(lFlags&(NOBOTTOM|NORIGHT)))
        pGC->DrawImagePart(CRect_d(mRect.r,mRect.b,mSize.x,mSize.y),CPoint_d(lDest.r,lDest.b),mImage);

}

/*namespace gui*/ } /*namespace buola*/ }
